<template>
  <div style="height: 50px">
    <el-button type="primary" @click="ParamEterizeSave">
      <el-icon><Plus /></el-icon>
      新增
    </el-button>
  </div>
  <el-table :data="tableData" style="width: 100%">
    <el-table-column prop="id" label="序号" />
    <el-table-column prop="paramName" label="名称" />
    <el-table-column prop="paramKey" label="键" />
    <el-table-column prop="paramValue" label="值" />
    <el-table-column prop="code" label="编码" />
    <el-table-column prop="type" label="类型" />
    <el-table-column prop="status" #default="scope">
      <el-button type="success" v-if="scope.row.status == 0" plain>
        正常
      </el-button>
      <el-button type="danger" v-if="scope.row.status == 1" plain>
        异常
      </el-button>
    </el-table-column>
    <el-table-column prop="paramType" label="参数类型" />
    <el-table-column label="操作">
      <template #default="scope">
        <div style="display: flex; align-items: center;color: blue;">
          <el-icon style="font-size: 15px;"><View /></el-icon>
          &nbsp;&nbsp;
          <el-text style="color: blue;" @click="ParamEterizeUpdate(scope.row)">
            查看
          </el-text>
          &nbsp;&nbsp;&nbsp;
          <el-icon style="font-size: 15px;"><DeleteFilled /></el-icon>
          &nbsp;&nbsp;
          <el-text
            style="color: blue;"
            @click="ParamEterizeDelete(scope.row.id)"
          >
            删除
          </el-text>
        </div>
      </template>
    </el-table-column>
  </el-table>
  <!-- 分页 -->
  <div class="demo-pagination-block">
    <el-pagination
      v-model:current-page="form.pageNum"
      v-model:page-size="form.pageSize"
      :page-sizes="[5, 10, 15, 20]"
      :small="small"
      :disabled="disabled"
      :background="background"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
  <!--添加弹框-->
  <el-dialog v-model="dialogFormVisible" title="查看界面" width="500">
    <el-form :model="formData">
      <el-form-item label="名称">
        <el-input v-model="formData.paramName" />
      </el-form-item>
      <el-form-item label="键">
        <el-input v-model="formData.paramKey" />
      </el-form-item>
      <el-form-item label="值">
        <el-input v-model="formData.paramValue" />
      </el-form-item>
      <el-form-item label="编码">
        <el-input v-model="formData.code" />
      </el-form-item>
      <el-form-item label="类型">
        <el-input v-model="formData.type" />
      </el-form-item>
      <el-form-item label="状态">
        <el-input v-model="formData.status" />
      </el-form-item>
      <el-form-item label="参数类型">
        <el-input v-model="formData.paramType" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="save()">添加</el-button>
      </span>
    </template>
  </el-dialog>

  <!--修改弹框-->
  <el-dialog v-model="dialogFormVisible2" title="查看界面" width="500">
    <el-form :model="formUpdate">
      <el-form-item label="名称">
        <el-input v-model="formUpdate.paramName" />
      </el-form-item>
      <el-form-item label="键">
        <el-input v-model="formUpdate.paramKey" />
      </el-form-item>
      <el-form-item label="值">
        <el-input v-model="formUpdate.paramValue" />
      </el-form-item>
      <el-form-item label="编码">
        <el-input v-model="formUpdate.code" />
      </el-form-item>
      <el-form-item label="类型">
        <el-input v-model="formUpdate.type" />
      </el-form-item>
      <el-form-item label="状态">
        <el-input v-model="formUpdate.status" />
      </el-form-item>
      <el-form-item label="参数类型">
        <el-input v-model="formUpdate.paramType" />
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script setup>
import { ref } from 'vue'
import request from '@/utils/request'
import { ElMessage } from 'element-plus'
const tableData = ref([])
// 分页 数据
const total = ref(10)
const small = ref(false)
const background = ref(false)
const disabled = ref(false)

const form = ref({
  pageNum: 1,
  pageSize: 5,
})
//分页方法
const handleCurrentChange = val => {
  form.value.pageNum = val
  ParamEterizeList()
}

const handleSizeChange = val => {
  form.value.pageSize = val
  ParamEterizeList()
}

function ParamEterizeList() {
  request({
    url: '/service-product/param/list',
    method: 'get',
    params: form.value,
  }).then(res => {
    tableData.value = res.data.records
    total.value = res.data.total
  })
}
ParamEterizeList()

const dialogFormVisible = ref(false)
const formData = ref({
  id: '',
  paramName: '',
  paramKey: '',
  paramValue: '',
  code: '',
  type: '',
  status: '',
  paramType: '',
})
function ParamEterizeSave() {
  dialogFormVisible.value = true
}
function save() {
  request({
    url: '/service-product/param/save',
    method: 'post',
    params: formData.value,
  }).then(res => {
    if (res.code === 200) {
      ElMessage.success(res.msg)
      dialogFormVisible.value = false
      formData.value = ref()
      ParamEterizeList()
    } else {
      ElMessage.error(res.msg)
    }
  })
}

const dialogFormVisible2 = ref(false)
const formUpdate = ref({
  id: '',
  paramName: '',
  paramKey: '',
  paramValue: '',
  code: '',
  type: '',
  status: '',
  paramType: '',
})
function ParamEterizeUpdate(row) {
  dialogFormVisible2.value = true
  formUpdate.value.id = row.id
  formUpdate.value.paramName = row.paramName
  formUpdate.value.paramKey = row.paramKey
  formUpdate.value.paramValue = row.paramValue
  formUpdate.value.code = row.code
  formUpdate.value.type = row.type
  formUpdate.value.status = row.status
  formUpdate.value.paramType = row.paramType
}
</script>
